package com.example.springboot01.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.springboot01.entity.Class;
import com.example.springboot01.req.PageQueryReq;
import com.example.springboot01.resp.enums.CommonRespEnum;
import com.example.springboot01.resp.enums.ProfessorRespEnum;
import com.example.springboot01.resp.Response;
import com.example.springboot01.service.ClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@Api(tags = "班级管理控制器", value = "班级管理控制器")
@RequestMapping("/api/v1/class")
public class ClassController {

    @Resource
    private ClassService classService;

    @PostMapping
    @ApiOperation(value = "添加班级")
    public Response add(@RequestBody Class clazz){
        classService.save(clazz);
        return Response.ok(null);
    }

    @PutMapping
    @ApiOperation(value = "更新班级")
    public Response update(@RequestBody Class clazz){
        classService.updateById(clazz);
        return Response.ok(null).setMsg("更新成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除班级信息")
    public Response delete(@PathVariable Integer id){
        if (!classService.removeClass(id)) {
            Response.response(ProfessorRespEnum.DELETE_ERROR);
        }
        return Response.response(CommonRespEnum.DELETE_OK);
    }

    @GetMapping
    @ApiOperation(value = "查询班级列表")
    public Response list(){
        return Response.ok(classService.listWithProfessor());
    }


    @GetMapping("/pro/{proId}")
    @ApiOperation(value = "查询班级列表")
    public Response listByPro(@PathVariable Integer proId){
        return Response.ok(classService.list(new LambdaQueryWrapper<Class>()
                .eq(Class::getProfessorId, proId)
        ));
    }


    @GetMapping("/page")
    public Response page(PageQueryReq req, HttpSession session){
        return Response.ok(this.classService.getPage(req, session));
    }
}
